package com.wzdigit.wms.wms.controller.TransferWareHouse;

import com.wzdigit.framework.exception.RestStatusException;
import com.wzdigit.framework.model.response.ErrorEntity;
import com.wzdigit.wms.basic.util.HttpSessionUtil;
import com.wzdigit.wms.basic.client.Pager;
import com.wzdigit.wms.basic.client.PagerInfo;
import com.wzdigit.wms.basic.client.request.asn.CloseRequest;
import com.wzdigit.wms.basic.client.request.asn.SrarchTransferWareHouseRequest;
import com.wzdigit.wms.basic.client.request.asn.UpdateAsnHeaderRequest;
import com.wzdigit.wms.basic.client.response.Result;
import com.wzdigit.wms.basic.client.response.asnHeader.TransferWareHouseDtlExt;
import com.wzdigit.wms.basic.client.response.asnHeader.TransferWareHouseExt;
import com.wzdigit.wms.wms.constant.WmsWareConstant;
import com.wzdigit.wms.wms.service.core.transfer.TransferHeaderService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.math.BigDecimal;
import java.util.List;

@RestController
@Api(tags = "转仓单管理")
@RequestMapping(value = WmsWareConstant.API_VERSION)
public class TransferWareHouseController {

    @Autowired
    @Lazy
    private TransferHeaderService transferHeaderService;

    private static final String REQUEST_MODEL = "/transferwarehouse";

    @ApiOperation(value = "转仓单列表", notes = "转仓单列表")
    @ApiResponses({@ApiResponse(code = 500, message = "系统出现异常", response = ErrorEntity.class)})
    @GetMapping(value = REQUEST_MODEL)
    public PagerInfo<List<TransferWareHouseExt>> searchTransferWareHouse(
    		SrarchTransferWareHouseRequest search, Pager<?> pager, 
    		HttpServletRequest servletRequest, HttpServletResponse servletResponse) {
        try {
            return transferHeaderService.searchTransferWareHouse(search, pager, servletRequest);
        } catch (RestStatusException ex) {
            servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            throw ex;
        }
    }

    @ApiOperation(value = "转仓单明细列表", notes = "转仓单明细列表")
    @ApiResponses({@ApiResponse(code = 500, message = "系统出现异常", response = ErrorEntity.class)})
    @GetMapping(value = REQUEST_MODEL + "/dtl")
    public PagerInfo<List<TransferWareHouseDtlExt>> earchTransferWareHouseDtl(
    		@ApiParam(value = "调拨单ID", required = true) 
    		@RequestParam(required = true) Integer transferHeaderId, Pager<?> pager, 
    		HttpServletRequest servletRequest, HttpServletResponse servletResponse) {
        try {
            return transferHeaderService.searchTransferWareHouseDtl(transferHeaderId, pager);
        } catch (RestStatusException ex) {
            servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            throw ex;
        }
    }

    @ApiOperation(value = "关闭转仓单", notes = "关闭转仓单")
    @ApiResponses({@ApiResponse(code = 500, message = "系统出现异常", response = ErrorEntity.class)})
    @RequestMapping(value = REQUEST_MODEL + "/close", method = RequestMethod.POST)
    public Result<Boolean> closeASN(
    		@RequestBody @Valid CloseRequest request, 
    		HttpServletRequest servletRequest, HttpServletResponse servletResponse) throws Exception {
        try {
            return transferHeaderService.close(request.getTransNos(), servletRequest);
        } catch (RestStatusException ex) {
            servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            throw ex;
        }
    }

    @ApiOperation(value = "编辑保存转仓单", notes = "编辑保存转仓单")
    @ApiResponses({@ApiResponse(code = 500, message = "系统出现异常", response = ErrorEntity.class)})
    @RequestMapping(value = REQUEST_MODEL + "/update", method = RequestMethod.POST)
    public Result<Boolean> updateASN(
    		@RequestBody @Valid UpdateAsnHeaderRequest request, 
    		HttpServletRequest servletRequest, 
    		HttpServletResponse servletResponse) throws Exception {
        try {
            return transferHeaderService.update(request, HttpSessionUtil.getLoginName(servletRequest));
        } catch (RestStatusException ex) {
            servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            throw ex;
        }
    }
    
	/**
	 * 导出转仓单
	 * 
	 * @param search
	 * @param servletRequest
	 * @param servletResponse
	 * @throws Exception
	 */
	@ApiOperation(value = "导出转仓单",notes = "导出转仓单")
    @ApiResponses({ @ApiResponse(code = 500, message = "系统出现异常", response = ErrorEntity.class) })
    @GetMapping(value = REQUEST_MODEL + "/export")
    public void export(SrarchTransferWareHouseRequest search, 
    		HttpServletRequest servletRequest, HttpServletResponse servletResponse) throws Exception {
        try {
        	transferHeaderService.export(search, servletRequest, servletResponse);
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }
    }
    
	/**
	 * 打印转仓单pdf
	 * @param servletRequest
	 * @param servletResponse
	 * @throws Exception
	 */
	@ApiOperation(value = "打印转仓单pdf",notes = "打印转仓单pdf")
    @ApiResponses({ @ApiResponse(code = 500, message = "系统出现异常", response = ErrorEntity.class) })
    @PostMapping(value = REQUEST_MODEL + "/print")
    public void print(
    		@ApiParam("调拨单ID集合") @RequestBody List<Integer> transferHeaderId,
    		HttpServletRequest servletRequest, 
            HttpServletResponse servletResponse) throws Exception {
        try {
        	transferHeaderService.print(transferHeaderId, servletRequest, servletResponse);
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }
    }

    /**
     * 打印转仓单pdf
     * @param servletRequest
     * @param servletResponse
     * @throws Exception
     */
    @ApiOperation(value = "打印转仓单(新)",notes = "打印转仓单(新)")
    @ApiResponses({ @ApiResponse(code = 500, message = "系统出现异常", response = ErrorEntity.class) })
    @PostMapping(value = REQUEST_MODEL + "/printNew")
    public Result printNew(
            @ApiParam("调拨单ID集合") @RequestBody List<Integer> transferHeaderId,
            HttpServletRequest servletRequest,
            HttpServletResponse servletResponse) throws Exception {
        try {
            return transferHeaderService.printNew(transferHeaderId, servletRequest, servletResponse);
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }
    }

	/**
	 * 打印转仓单pdf
	 * @param transferNo
	 * @param servletRequest
	 * @param servletResponse
	 * @throws Exception
	 */
	/*@ApiOperation(value = "打印转仓单pdf",notes = "打印转仓单pdf")
    @ApiResponses({ @ApiResponse(code = 500, message = "系统出现异常", response = ErrorEntity.class) })
    @GetMapping(value = REQUEST_MODEL + "/print")
    public void print(@ApiParam(value = "调拨单ID", required = true) 
			@RequestParam(required = true) Integer transferHeaderId,
    		HttpServletRequest servletRequest, 
            HttpServletResponse servletResponse) throws Exception {
        try {
        	transferHeaderService.print(transferHeaderId, servletRequest, servletResponse);
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }
    }*/

	//TODO ZYF:导出调拨SN
    @ApiOperation(value = "导出SN",notes = "导出SN")
    @ApiResponses({ @ApiResponse(code = 500, message = "系统出现异常", response = ErrorEntity.class) })
    @GetMapping(value = REQUEST_MODEL + "/exportSNList")
    public void exportSNList(BigDecimal headerId,
                       HttpServletRequest servletRequest, HttpServletResponse servletResponse) throws Exception {
        try {
            transferHeaderService.exportSNList(headerId, servletRequest, servletResponse);
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }
    }

}